package com.iye.puretennis.helper;

import android.content.Context;
import com.iye.puretennis.R;
import com.iye.puretennis.domain.UrlSuffix;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2015/1/13
 * Time: 12:34
 */
public class ImageHelper {

    public static final String CACHE_DIRECTORY = "date";

    public static final int STUB_IMAGE = R.drawable.avatar;
    public static final int IMAGE_FOR_EMPTY_URI = R.drawable.avatar;


    public static DisplayImageOptions.Builder createImageBuilder() {
        return new DisplayImageOptions.Builder().showStubImage(STUB_IMAGE).showImageForEmptyUri(IMAGE_FOR_EMPTY_URI).cacheInMemory(true).cacheOnDisk(true);
    }

    public static DisplayImageOptions createDisplayImageOptions(DisplayImageOptions.Builder builder, String userGuid) {
        return builder.extraForDownloader(userGuid).showStubImage(STUB_IMAGE).showImageForEmptyUri(IMAGE_FOR_EMPTY_URI).cacheInMemory(true).cacheOnDisk(true).build();
    }

    public static void initImageLoader(ImageLoader imageLoader, Context context) {

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, CACHE_DIRECTORY);

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(5)
                .threadPriority(Thread.NORM_PRIORITY - 1).tasksProcessingOrder(QueueProcessingType.LIFO).denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(1024 * 1024)).memoryCacheSize(1 * 1024 * 1024).discCache(new UnlimitedDiscCache(cacheDir)) // default
                .discCacheSize(20 * 1024 * 1024).discCacheFileCount(400).discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
//                .imageDownloader(new CustomImageDownloader(context, token)) // default
                .imageDecoder(new BaseImageDecoder(true))// default

                .build();

        //初始化配置
        imageLoader.init(config);


    }

    public static String getImageUri(String userGuid) {
        return RemoteUrlCollections.getUrl(UrlSuffix.USER_IMAGE_DOWNLOAD) + "?guid=" + userGuid;
    }
}
